package com.wangshuqi.jdbc.test;

import java.util.concurrent.*;

public class ThreadPoolUtil {

    private int corePoolSize = 5; // 核心线程数

    private int maximumPoolSize = 15; // 最大线程数

    private long keepAliveTime = 60; // 当线程空闲时间达到keepAliveTime时，线程会退出，直到线程数量=corePoolSize，如果allowCoreThreadTimeout=true，则会直到线程数量=0

    private TimeUnit unit = TimeUnit.SECONDS; // keepAliveTime 时间单位，这里用秒

    private BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(10); // 任务队列类型，长度10

    private ThreadPoolUtil() {
    }

    /**
     * 获取线程池
     *
     * @param
     * @return java.util.concurrent.ExecutorService
     * @author codewsq
     * @date 2022/4/5 0005 23:44
     */
    public static ExecutorService getThreadPool() {
        ThreadPoolUtil poolUtil = new ThreadPoolUtil();
        return poolUtil.createThreadPool();
    }

    /**
     * 创建一个线程池
     *
     * @author codewsq
     * @date 2022/4/5 0005 23:42
     */
    private ThreadPoolExecutor createThreadPool() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
        return threadPoolExecutor;
    }
}
